package hirt;

import java.awt.Point;
import java.nio.ByteBuffer;
import java.util.Vector;

/**
 * MoveableItem derives from the Item2D class. It adds the ability for objects
 * to have two dimensional velocity and implies self controlled movement.
 * @author puzzud
 */
public class MoveableItem extends Item2D
{
    /**
     * Two dimensional velocity.
     */
    Point velocity;

    /**
     * Default constructor that sets velocity to (0,0).
     */
    public MoveableItem()
    {
        super();

        velocity = new Point( 0, 0 );
    }

    /**
     * Builds an Item from a byte stream, presumably created by an item across the network
     * this item should be identical to that one
     * @param bytes
     * @param items: Master Item List used to build the Item
     */
    public MoveableItem(ByteBuffer bytes, Vector<Item> items)
    {
        fromBytes(bytes,items);

    }

    /**
     * Builds an Item from a save string, presumably created by an item when saving to a file
     * this item should be identical to that one
     * @param saveString
     * @param items: Master Item List used to build the Item
     */
    public MoveableItem(String text, Vector<Item> items)
    {
        fromSaveString(text,items);
    }

    public Point getVelocity()
    {
        return( velocity );
    }

    public int getXVelocity()
    {
        return( velocity.x );
    }

    public int getYVelocity()
    {
        return( velocity.y );
    }

    public void setVelocity( Point velocity )
    {
        this.velocity = velocity;
    }

    public void setVelocity( int xVelocity, int yVelocity )
    {
        velocity.x = xVelocity;
        velocity.y = yVelocity;
    }

    public void setXVelocity( int xVelocity )
    {
        velocity.x = xVelocity;
    }

    public void setYVelocity( int yVelocity )
    {
        velocity.y = yVelocity;
    }

    /**
     * Reposition this moveable item by its current velocity and current position.
     */
    public void move()
    {
        this.position.x += velocity.x;
        this.position.y += velocity.y;
    }

    /**
     * Returns a ByteBuffer representation of this Item
     * Used to send items over the network
     * @return ByteBuffer
     */
    @Override
    public ByteBuffer toByte()
    {
        ByteBuffer buf = super.toByte();
        buf.put(ByteChanger.pointToByte(velocity));
        return buf;

    }

    /**
     * Builds an Item from a byte stream, presumably created by an item across the network
     * this item should be identical to that one
     * @param bytes
     * @param items: Master Item List used to build the action
     * @return what's left of the parem bytes after this method is completes
     */
    @Override
    protected ByteBuffer fromBytes(ByteBuffer bytes, Vector<Item> items)
    {
        bytes = super.fromBytes(bytes, items);
        velocity = ByteChanger.getPoint(bytes, 0);
        try {
            bytes = ByteChanger.truncate(bytes, 8);
        } catch (Exception ex) {

        }
        return bytes;
    }

    /**
     * Returns a String representation of this Item
     * Used to save this item to a file
     * @return Save String
     */
    @Override
    public String toSaveString()
    {
        String text = super.toSaveString();
        text += velocity.x + "\t";
        text += velocity.y + "\t";
        return text;
    }

    /**
     * Builds an Item from a save string, presumably read from a save
     * this item should be identical to the one saved in the file
     * @param save string
     * @param items: Master Item List used to build the action
     * @return what's left of the parem SaveString after this methods completes
     */
    @Override
    protected String fromSaveString(String text, Vector<Item> items)
    {
        text = super.fromSaveString(text, items);
        String substring = ByteChanger.getNextValue(text);
        int x = Integer.parseInt(substring);

        text = ByteChanger.incrementString(text);
        substring = ByteChanger.getNextValue(text);
        int y = Integer.parseInt(substring);
        velocity = new Point(x,y);

        text = ByteChanger.incrementString(text);
        return text;
    }
}
